package xin.yogee.ms.module.oa.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import xin.yogee.ms.common.BaseController;
import xin.yogee.ms.module.oa.entity.InputRecord;
import xin.yogee.ms.module.oa.service.IInputRecordService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 入库记录 前端控制器
 * </p>
 *
 * @author jackieQ
 * @since 2020-02-28
 */
@RestController
@RequestMapping("/oa/input")
public class InputRecordController extends BaseController {

    @Autowired
    private IInputRecordService inputRecordService;

    @RequestMapping(value = "/get")
    public String get(@RequestBody JSONObject json){
        String id = json.getString("id");
        InputRecord inputRecord = inputRecordService.getById(id);
        return successJson("success",inputRecord,"获取成功");
    }

    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request){
        String id = request.getParameter("id");
        String current = request.getParameter("currentPage") == null ? "1" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize");
        IPage<InputRecord> inputList = inputRecordService.getList(new Page<>(Long.valueOf(current),Long.valueOf(pageSize)),id);

        return successJson("success",inputList,"获取成功");
    }

    @RequestMapping(value = "/store")
    @ResponseBody
    public String store(@RequestBody JSONObject json, InputRecord inputRecord){
        String id = json.getString("id");
        String productId = json.getString("productId");
        String stockId = json.getString("stockId");
        String counts = json.getString("counts");
        String buyAt = json.getString("buyAt");
        String price = json.getString("price");
        String total = json.getString("total");
        inputRecord.setId(id);
        inputRecord.setProductId(productId);
        inputRecord.setStockId(stockId);
        inputRecord.setBuyAt(buyAt);
        inputRecord.setPrice(price);
        inputRecord.setTotal(total);
        inputRecord.setCounts(counts);
        inputRecordService.saveOrUpdate(inputRecord);
        return successJson("success", new HashMap(0), "创建成功");
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody JSONObject json){
        String id = json.getString("inputRecordId");
        inputRecordService.removeById(id);
        return successJson("success","","删除成功");
    }
}
